with Ada.Numerics.Discrete_Random;
with Stations; use Stations;
package body Folk is

   CurrentPassengerId: PassengerID;
   TimeGenerator: RandomTime.Generator;
   Station: RandomStations.Generator;

   protected body GOD is

      procedure initialization(id: out PassengerId) is
      begin

         CurrentPassengerId := CurrentPassengerId+1;
         id := CurrentPassengerId;

      end initialization;

   end GOD;

   task body Passenger is

      id: PassengerId;
      stationFrom: StationType;
      stationTo: StationType;
      waiting: WaitingTime;
      flag: Boolean;

   begin

      -- imenuem ludjej
      GOD.initialization(id);

      -- generiruem vremja rozdenija
      waiting:=RandomTime.Random(TimeGenerator);
      delay duration(waiting);

      -- opredeljaem stancii ot kuda i kuda poedit pasazir
      stationFrom:=RandomStations.Random(Station);
      stationTo:=RandomStations.Random(Station);

      Put_Line("Passenger "&Integer'Image(id)&" come to Station "
               &StationsList(stationFrom).getName&"; Destination station is Station "&StationsList(stationTo).getName);

      -- Dobavljaem pasazira v o4eredj na stanciju
      StationsList(stationFrom).add(id);

      -- zdjom pribitija poezda
      accept MoveToTrain;
      flag := false;
      -- edim v poezde poka ne budet nasha stancija
      loop
         accept LeaveTrain(fl: out Boolean; station: in StationType) do

            if station = stationTo then
               fl:=true;
               flag:=true;
            else
               fl:=false;
            end if;

         end LeaveTrain;
         if flag then
            exit;
         end if;

      end loop;


      -- Passazir pribil!
      Put_Line("Passenger"&Integer'Image(id)&" arrived to Station"&StationsList(stationTo).getName);

   end Passenger;

end Folk;
